package com.erp.gather.controller;

import com.erp.gather.domain.ErpCollectProductSkuProValue;
import com.erp.gather.domain.ErpCollectProductSkuProperty;
import com.erp.gather.domain.bo.ErpCollectProductSkuPropertyBo;
import com.erp.gather.domain.vo.ErpCollectProductSkuPropertyVo;
import com.erp.gather.mapper.ErpCollectProductSkuProValueMapper;
import com.erp.gather.mapper.ErpCollectProductSkuPropertyMapper;
import com.erp.gather.service.IErpCollectProductSkuPropertyService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.redis.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采集商品SKU属性
 *
 * @author erp
 * @date 2023-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/collectProductSkuProperty")
public class ErpCollectProductSkuPropertyController extends BaseController {

    private final IErpCollectProductSkuPropertyService iErpCollectProductSkuPropertyService;
    private final ErpCollectProductSkuPropertyMapper collectProductSkuPropertyMapper;
    private final ErpCollectProductSkuProValueMapper collectProductSkuProValueMapper;

    /**
     * 查询采集商品SKU属性列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpCollectProductSkuPropertyVo> list(ErpCollectProductSkuPropertyBo bo, PageQuery pageQuery) {
        return iErpCollectProductSkuPropertyService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取采集商品SKU属性详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpCollectProductSkuPropertyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpCollectProductSkuPropertyService.queryById(id));
    }

    /**
     * 新增采集商品SKU属性
     */
    @RepeatSubmit()
    @PostMapping()
    public R<ErpCollectProductSkuPropertyBo> add(@Validated(AddGroup.class) @RequestBody ErpCollectProductSkuPropertyBo bo) {
    	iErpCollectProductSkuPropertyService.insertByBo(bo);
        return R.ok(bo);
    }

    /**
     * 修改采集商品SKU属性
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpCollectProductSkuPropertyBo bo) {
        return toAjax(iErpCollectProductSkuPropertyService.updateByBo(bo));
    }

    /**
     * 删除采集商品SKU属性
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpCollectProductSkuPropertyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * @Description: 根据属性值，获取属性ID
     * @Author: j.zh
     * @Date: 2024/6/12
     * @param value
     * @return
     * @Version: 4.0.0
    **/
    @GetMapping("/getValueById/{type}/{value}")
    public String getValueById(@PathVariable("type") String type, @PathVariable("value") String value) {
        return iErpCollectProductSkuPropertyService.getValueById(type, value);
    }

    /**
     * @Description: 每次调用该方法，将sku属性和值全部放入缓存中，P开头为属性名，PV开头为属性值
     * @Author: j.zh
     * @Date: 2024/6/14
     * @return
     * @Version: 4.0.0
    **/
    @GetMapping("/getValueList")
    public R<Void> getValueList() {
        List<ErpCollectProductSkuProperty> skuPropertyList = collectProductSkuPropertyMapper.selectList();
        List<ErpCollectProductSkuProValue> skuProValueList = collectProductSkuProValueMapper.selectList();
        for (ErpCollectProductSkuProperty skuProperty : skuPropertyList){
            if(!RedisUtils.isExistsObject("P_"+skuProperty.getPropertyName())){
                RedisUtils.setCacheObject("P_"+skuProperty.getPropertyName(), skuProperty.getId().toString());
            }
        }
        for (ErpCollectProductSkuProValue skuPropertyValue : skuProValueList){
            if(!RedisUtils.isExistsObject("PV_"+skuPropertyValue.getPropertyValue())){
                RedisUtils.setCacheObject("PV_"+skuPropertyValue.getPropertyValue(), skuPropertyValue.getId().toString());
            }
        }
        return R.ok();
    }
}
